package com.wsjzzcbq.action.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsjzzcbq.action.OrderServiceCreateAction;
import com.wsjzzcbq.bean.Order;
import com.wsjzzcbq.service.OrderService;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * OrderServiceCreateActionImpl
 *
 * @author wsjz
 * @date 2023/10/16
 */
@Slf4j
@Component
public class OrderServiceCreateActionImpl implements OrderServiceCreateAction {

    @Autowired
    private OrderService orderService;

    @Override
    public void create(String userId, int money, String orderCode, boolean rollback) {

        Order order = new Order();
        order.setCode(orderCode);
        order.setCount(1);
        order.setUserId(userId);
        order.setMoney(money);

        orderService.save(order);
    }

    @Override
    public boolean commit(BusinessActionContext businessActionContext) {
        System.out.println("money");
        System.out.println(businessActionContext.getActionContext("money"));
        System.out.println(businessActionContext.getActionContext("orderCode"));
        log.info("提交成功");
        return true;
    }

    @Override
    public boolean cancel(BusinessActionContext businessActionContext) {
        String orderCode = String.valueOf(businessActionContext.getActionContext("orderCode"));
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", orderCode);
        orderService.remove(queryWrapper);
        log.info("回滚成功");
        return true;
    }
}
